package com.android.fastride.view.fragment;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;

import com.android.fastride.R;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DatePickerFragment extends DialogFragment {
    private static final String TAG = "DatePickerFragment";
    private static final String ARG_DATE = "date";
    private static final String ARG_MAX_DATE = "maxDate";
    private static final String ARG_MIN_DATE = "minDate";
    public static final String EXTRA_DATE = "extra_date";
    private DatePicker mDatePicker;
    private SelectedDateListener mSelectedDateListener;
    private int mActivityRequestCode = -2;

    public void setSelectedDateListener(SelectedDateListener selectedDateListener,
                                        int activityRequestCode) {
        mActivityRequestCode = activityRequestCode;
        mSelectedDateListener = selectedDateListener;
    }

    public static DatePickerFragment newInstance(Date date) {
        return newInstance(date, null, null);
    }

    /**
     * @param date 当前显示时间
     * @param maxDate 可选最大时间
     * @param mixDate 可选最小时间
     * @return
     */
    public static DatePickerFragment newInstance(Date date, Date maxDate, Date mixDate) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(ARG_DATE, date);
        bundle.putSerializable(ARG_MAX_DATE, maxDate);
        bundle.putSerializable(ARG_MIN_DATE, mixDate);
        DatePickerFragment datePickerFragment = new DatePickerFragment();
        datePickerFragment.setArguments(bundle);
        return datePickerFragment;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        Date date = (Date) bundle.getSerializable(ARG_DATE);
        Date maxDate = (Date) bundle.getSerializable(ARG_MAX_DATE);
        Date minDate = (Date) bundle.getSerializable(ARG_MIN_DATE);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_dialog_date, null);
        mDatePicker = (DatePicker) view.findViewById(R.id.dialog_date_picker);
        if(maxDate != null) {
            mDatePicker.setMaxDate(maxDate.getTime());
        }
        if (minDate != null) {
            mDatePicker.setMinDate(minDate.getTime());
        }
        mDatePicker.setMaxDate(new Date().getTime());
        mDatePicker.init(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), null);
        // 1.加载布局
        return new AlertDialog.Builder(getActivity())
                .setTitle("")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int year = mDatePicker.getYear();
                        int month = mDatePicker.getMonth();
                        int day = mDatePicker.getDayOfMonth();
                        Date date = new GregorianCalendar(year, month, day).getTime();
                        sendResult(Activity.RESULT_OK, date);
                    }
                })
                .setView(view)
                .setCancelable(true)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dismiss();
                    }
                })
                .create();
    }

    private void sendResult(int resultCode, Date date) {
        if (getTargetFragment() == null && mSelectedDateListener == null) {
            return;
        }
        if(getTargetFragment() != null) {
            Intent data = new Intent();
            data.putExtra(EXTRA_DATE, date);
            getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, data);
        } else {
            mSelectedDateListener.onSelectedDate(mActivityRequestCode,date);
        }
    }

    public interface SelectedDateListener{
        public void onSelectedDate(int requestCode,Date date);
    }
}
